#include "GUISound.h"

void GUISound::Init(string path_of_text, Coords location)
{
	// Location / Size of Screen
	m_location.x = location.x;
	m_location.y = location.y;
	m_dimensions.w = 300;
	m_dimensions.h = 500;

	// Location / Size of "Mini screen"
	m_innerBox.w = m_dimensions.w - 150;
	m_innerBox.h = m_dimensions.h - 50;
	m_innerBoxStart.x = m_location.x + 25;
	m_innerBoxStart.y = m_location.y + 25;

	// Location / Size of Buttons
	m_buttonsStart.x = m_innerBoxStart.x + 10;
	m_buttonsStart.y = m_innerBoxStart.y + 10;
	m_buttonsDimensions.w = m_innerBox.w - 20;
	m_buttonsDimensions.h = V_DISTANCE_BETWEEN_SONGS;

	// Memory Mangement / Speed increases
	tracks.reserve(32);
	tracks_names.reserve(32);
}

GUISound::~GUISound()
{
}

void GUISound::AddSound(string filename, string sound_name)
{
	tracks.resize(tracks.size() + 1);
	tracks[tracks.size() - 1].SetText(filename.c_str()); // button text
	tracks[tracks.size() - 1].SetXY((m_buttonsStart.x), (m_buttonsStart.y + ((tracks.size() - 1) * V_DISTANCE_BETWEEN_SONGS)),
		(m_buttonsStart.x + m_buttonsDimensions.w), (m_buttonsStart.y + m_buttonsDimensions.h + ((tracks.size() - 1) * V_DISTANCE_BETWEEN_SONGS)) ); // button location
	tracks[tracks.size() - 1].SetImages("b_Game_Sound", "b_Game_Sound_m"); // button gfx

	tracks_names.resize(tracks_names.size() + 1);
	tracks_names[tracks_names.size() - 1] = sound_name;
}

void GUISound::Update()
{
	//m_location.x += ClipPlane->GetX();
	//m_location.y += ClipPlane->GetY();
	for(unsigned int i = 0; i < tracks.size(); ++i)
		tracks[i].Update();
}

void GUISound::HandleEvents(SDL_Event& event)
{

	if(event.type == SDL_MOUSEBUTTONDOWN)
	{
		if(event.button.button == SDL_BUTTON_LEFT)
		{
			// Gets our current mouse location and update them for the grid's current location
			Coords mouseOffsets(event.button.x, event.button.y);
			for(unsigned int i = 0; i < tracks.size(); ++i)
				if(tracks[i].inBounds(mouseOffsets.x, mouseOffsets.y))
				{
					SoundPlay->Play(tracks_names[i].c_str());
				}
		}
	}
}

void GUISound::HandleGraphicalEvents(SDL_Event& event)
{

	if(event.type == SDL_MOUSEMOTION)
	{

		// Gets our current mouse location and update them for the grid's current location
		Coords mouseOffsets(event.motion.x, event.motion.y);

		for(unsigned int i = 0; i < tracks.size(); ++i)
			if(tracks[i].inBounds(mouseOffsets.x, mouseOffsets.y))
			{
				for(unsigned int j = 0; j < tracks.size(); ++j)
					tracks[j].SetNameGraphic();
				// switch songs
				tracks[i].SetMouseOverGraphic();
			}
	}
}

void GUISound::DrawTrans()
{
	if(m_active)
	{
		Coords start, end;
		start.x = (m_location.x + m_dimensions.w / 2) - 55;
		start.y = (m_location.y + 10);

		//boxRGBA(Entity::GetScreen(), m_location.x, m_location.y, m_location.x + m_dimensions.w, m_location.y + m_dimensions.h, 100, 0, 0, 255);
		OGL_Color col = { 0.0f, 0.0f, 0.0f, 20.0f };
		DrawPrimitives->DrawRectangle(m_location.x, m_location.y, m_location.x + m_dimensions.w, m_location.y + m_dimensions.h, col);
		//TextDisplay->DisplayMessage("Sound GUI", m_font, blue, start.x, start.y, FONT_SIZE_LARGE);

		// The start and end of the inner "grid"
		start.x = m_innerBoxStart.x;
		start.y = m_innerBoxStart.y;
		end.x =	m_innerBoxStart.x +  m_innerBox.w;
		end.y =	m_innerBoxStart.y + m_innerBox.h;

		OGL_Color color = {0.0f, 100.0f / 255.0f, 1.0f, 200.0f / 255.0f};
		DrawPrimitives->DrawRectangle(start.x, start.y, end.x, end.y, col);


	}
}

void GUISound::DrawOpaq()
{
	// Only draw if the GUISound is active
	if(m_active)
	{
		Coords start;
		start.x = (m_location.x + m_dimensions.w / 2) - 55;
		start.y = (m_location.y + 10);

		//TextDisplay->DisplayMessage("Sound GUI", m_font, blue, start.x, start.y - 5, FONT_SIZE_MEDIUM);

		//boxRGBA(Entity::GetScreen(), m_location.x, m_location.y, m_location.x + m_dimensions.w, m_location.y + m_dimensions.h, 100, 0, 0, 255);
		/*OGL_Color col = { 100 / 255.0, 0, 0, .5 };
		DrawPrimitives->DrawRectangle(m_location.x, m_location.y, m_location.x + m_dimensions.w, m_location.y + m_dimensions.h, col);
		

		// The start and end of the inner "grid"
		
		end.x =	m_innerBoxStart.x +  m_innerBox.w;
		end.y =	m_innerBoxStart.y + m_innerBox.h;
		//boxRGBA(Entity::GetScreen(), start.x, start.y, end.x, end.y, 0, 100, 255, 80);
		OGL_Color color = {0, 100 / 255.0, 1, 80 / 255.0};
		DrawPrimitives->DrawRectangle(start.x, start.y, end.x, end.y, col);
		//boxRGBA(Entity::GetScreen(), x1, y1, x2 - 1, y2 - 1, 255, 0, 0, 70);*/

		start.x = m_innerBoxStart.x;
		start.y = m_innerBoxStart.y;
	
		
		

		// Display songs
		start.x += 10;
		start.y += 10;
		int tempY = start.y;
		for(unsigned int i = 0; i < tracks.size(); ++i)
		{
			tracks[i].applySurface();
			//TextDisplay->DisplayMessage(tracks[i].GetText(), m_font, black, tracks[i].GetMidX(FONT_SIZE_LARGE), tracks[i].GetMidY(FONT_SIZE_SMALL), FONT_SIZE_SMALL - 1);
			//TextDisplay->DisplayMessage(tracks[i].GetText(), m_font, black, start.x, start.y);
			
			//start.y += V_DISTANCE_BETWEEN_SONGS;

			if(i == 15)
				break; // can't display any more songs
		}


		


	}
}